package component.fileupload.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.baidu.inf.iis.bcs.model.ObjectMetadata;

import component.fileupload.exception.FileUploadException;
import component.fileupload.listener.FileUploadStatus;
import component.fileupload.util.BcsUtil;
import component.fileupload.vo.UploadFileVO;

public class BaiduFileUploadAction extends ServletFileUploadAction {

	public String doUpload(ServletConfig config, HttpServletRequest request,
			HttpServletResponse response) throws FileUploadException {
		HttpSession session = request.getSession();
		String isUpload = "true";

		if (fileInfoMap == null) {
			this.getInputStreams(config, request, response);
		}

		// 获取相对路径
		GregorianCalendar now = new GregorianCalendar();
		saveFilePath = request.getParameter("saveFilePath");
		if(saveFilePath == null) {
			// 图片所在文件夹,bcs会在指定的bucket下自动创建
			saveFilePath = "/" + now.get(Calendar.YEAR) + "/" + (now.get(Calendar.MONTH) < 9 ? "0" + (now.get(Calendar.MONTH) + 1) : "" + (now.get(Calendar.MONTH) + 1));
		}

		try {
			for (int i = 0; i < fileInfoMap.size(); i++) {
				UploadFileVO vo = (UploadFileVO) fileInfoMap.get(new Integer(i));
				String originalFilename = vo.getFileName();
				String fileName = String.valueOf(now.getTimeInMillis());// 上传后要指定的文件名
	
				InputStream in = vo.getInputStream();
				ObjectMetadata metadata = new ObjectMetadata();// 文件meta信息
				metadata.setContentLength(vo.getFileSize()); // 必须设置文件大小
				
				if(originalFilename.lastIndexOf('.') != -1) { 
					String suffix = originalFilename.substring(originalFilename.lastIndexOf('.') + 1);
					if(suffix != null) {
						fileName += "." + suffix;
						// 文件类型，如：image/jpeg、image/gif，可不设置，图片类的最好设置，若不指定，则会直接下载图片，而不是在页面上打开
						suffix = suffix.toLowerCase();
						if(suffix.equals("jpeg") || suffix.equals("jpg") || suffix.equals("gif") || suffix.equals("png") ){
							metadata.setContentType("image/" + suffix);
						}
					}
				}
							
				String filePath = saveFilePath + "/" + fileName;// 相对于bucket的文件路径
				String serverSaveFilePath = BcsUtil.upload(in, metadata, filePath);
				vo.setServerSaveFilePath(serverSaveFilePath);
				vo.setDbSaveFilePath(serverSaveFilePath);
				isUpload = serverSaveFilePath;
			}
		}catch(Exception e) {
			e.printStackTrace();
			isUpload = "false";
			listener.error(e.getMessage());
			throw new FileUploadException(e);
		}
		
		//设置完成状态
		FileUploadStatus fileUploadStatus = new FileUploadStatus();
		fileUploadStatus.setCurrentStatus(FileUploadStatus.completeStatus);
		session.setAttribute("FILE_UPLOAD_STATS",fileUploadStatus);
		
		session.setAttribute("fileInfoMap", fileInfoMap);

		// 停止使用监听器
		listener.done();
		System.out.println("停止监听");

		return isUpload;
	}

}
